package com.zeldu.mail.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;
@Service
public class SendService {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Value("${spring.mail.username}")
    private String username;
    @Async
    public void testSendSimple(String targetEmail) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(username);
        message.setTo(targetEmail);
        message.setSubject("zleduApp注册提醒您,注册码为:");
        int code=new Random().nextInt(899999) + 100000;
        message.setText(String.valueOf(code));
        javaMailSender.send(message);
        //设置过期时间为10分钟
        redisTemplate.opsForValue().set(targetEmail, String.valueOf(code),600, TimeUnit.SECONDS);
    }
}
